using System;
using SimpleJSON;
using UnityEngine;

namespace Gj
{
    public class CompareHelper : NodeHelper
    {
        public BindDataType dataType = BindDataType.Bool;
        public JudgeAction judge = JudgeAction.Equal;
        public bool withKey;
        public string judgeValue;
        public bool dateTime;

        protected override BindDataType bindDataType => dataType;

        protected override void BindData(string s)
        {
            if (debug) LogTools.Log("BindData: {0}", s);
            var value = withKey ? key : judgeValue;
            // if (StringTools.IsEmpty(value)) return;
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = value.Equals(s);
                    break;
                case JudgeAction.UnEqual:
                    b = !value.Equals(s);
                    break;
            }

            if (b)
                Show();
            else
                Hide();
        }

        protected override void BindData(float f)
        {
            if (debug) LogTools.Log("BindData: {0}", f);
            if (StringTools.IsEmpty(judgeValue)) return;
            var value = StringTools.ToFloat(judgeValue);
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = f == value;
                    break;
                case JudgeAction.UnEqual:
                    b = f != value;
                    break;
                case JudgeAction.Greater:
                    b = f > value;
                    break;
                case JudgeAction.Less:
                    b = f < value;
                    break;
                case JudgeAction.GreaterOrEqual:
                    b = f >= value;
                    break;
                case JudgeAction.LessOrEqual:
                    b = f <= value;
                    break;
            }

            if (b)
                Show();
            else
                Hide();
        }

        protected override void BindData(bool bb)
        {
            if (debug) LogTools.Log("BindData: {0}", bb);
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = bb;
                    break;
                case JudgeAction.UnEqual:
                    b = !bb;
                    break;
            }

            if (b)
                Show();
            else
                Hide();
        }

        protected override void BindData(int i)
        {
            if (debug) LogTools.Log("BindData: {0}", i);
            if (judge == JudgeAction.Timer || judge == JudgeAction.CountDown) judgeValue = "0";
            if (!StringTools.IsNum(judgeValue)) return;
            var value = StringTools.ToInt(judgeValue);
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = i == value;
                    break;
                case JudgeAction.UnEqual:
                    b = i != value;
                    break;
                case JudgeAction.Greater:
                    b = i > value;
                    break;
                case JudgeAction.Less:
                    b = i < value;
                    break;
                case JudgeAction.GreaterOrEqual:
                    b = i >= value;
                    break;
                case JudgeAction.LessOrEqual:
                    b = i <= value;
                    break;
                case JudgeAction.Timer:
                    b = i > (dateTime ? DateTools.GetNowTime() : Time.time);
                    break;
                case JudgeAction.CountDown:
                    b = (dateTime ? DateTools.GetNowTime() : Time.time) > i;
                    break;
            }

            if (debug) LogTools.Log("{0} {1} {2}", i, value, b);
            if (b)
                Show();
            else
                Hide();
        }

        protected override void BindData(double d)
        {
            if (debug) LogTools.Log("BindData: {0}", d);
            if (judge == JudgeAction.Timer || judge == JudgeAction.CountDown) judgeValue = "0";
            if (!StringTools.IsNum(judgeValue)) return;
            double value = StringTools.ToInt(judgeValue);
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = d == value;
                    break;
                case JudgeAction.UnEqual:
                    b = d != value;
                    break;
                case JudgeAction.Greater:
                    b = d > value;
                    break;
                case JudgeAction.Less:
                    b = d < value;
                    break;
                case JudgeAction.GreaterOrEqual:
                    b = d >= value;
                    break;
                case JudgeAction.LessOrEqual:
                    b = d <= value;
                    break;
                case JudgeAction.Timer:
                    b = Convert.ToInt32(d / 1000) > (dateTime ? DateTools.GetNowTime() : Time.time);
                    break;
                case JudgeAction.CountDown:
                    b = (dateTime ? DateTools.GetNowTime() : Time.time) > Convert.ToInt32(d / 1000);
                    break;
            }

            if (debug) LogTools.Log("{0} {1} {2}", d, value, b);
            if (b)
                Show();
            else
                Hide();
        }

        protected override void BindData(JSONObject obj)
        {
            if (debug) LogTools.Log("BindData: {0}", obj);
            var value = withKey ? key : judgeValue;
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = obj.Count > 0;
                    break;
                case JudgeAction.UnEqual:
                    b = obj.Count == 0;
                    break;
                case JudgeAction.Check:
                    b = obj.Count > 0 && obj[value] != null;
                    break;
            }

            if (b)
                Show();
            else
                Hide();
        }

        protected override void BindData(JSONArray arr)
        {
            if (debug) LogTools.Log("BindData: {0}", arr);
            if (StringTools.IsEmpty(judgeValue)) return;
            var i = arr.Count;
            var value = StringTools.ToInt(judgeValue);
            var b = false;
            switch (judge)
            {
                case JudgeAction.Equal:
                    b = i == value;
                    break;
                case JudgeAction.UnEqual:
                    b = i != value;
                    break;
                case JudgeAction.Greater:
                    b = i > value;
                    break;
                case JudgeAction.Less:
                    b = i < value;
                    break;
                case JudgeAction.GreaterOrEqual:
                    b = i >= value;
                    break;
                case JudgeAction.LessOrEqual:
                    b = i <= value;
                    break;
            }

            if (b)
                Show();
            else
                Hide();
        }

        public virtual void Show()
        {
        }

        public virtual void Hide()
        {
        }
    }
}